package com.sls.web.manager.recharge.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Adjustment;
import com.sls.web.manager.recharge.pojo.Orders;
import com.sls.web.manager.recharge.service.AdmjustmentService;
import com.sls.web.manager.socket.SuperService;

@Service("admjustmentService")
public class AdmjustmentServiceImpl extends SuperService implements AdmjustmentService {
	private Gson gson = CommonUtil.gson();

	@Override
	public List<Orders> findOreders(Orders orders, UserInfoVO userinfo) throws Exception {
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.FIND_ORDER);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		dto = super.send(head, orders);
		if (TransmissionCode.SUCCESS.getCode().equals(dto.getHead().getResponseCode())) {
			return gson.fromJson(dto.getBody(), new TypeToken<List<Orders>>() {
			}.getType());
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void eto(Adjustment a, UserInfoVO userinfo) throws Exception {
		if (a.getFromMobile() == null ||a.getFromMobile().equals("")|| a.getType() == null) {
			throw new ManagerException(Constants.ADJUST_ERRO);
		}
		if (!a.getType()) {
			if (a.getToMobile() == null||a.getToMobile().equals("")) {
				throw new ManagerException(Constants.ADJUST_ERRO);
			}
		}
		SocketDTO dto = new SocketDTO();
		Head head = new Head();
		head.setServiceCode(Constants.ETO);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		dto = super.send(head, a);
		if (TransmissionCode.DUPLICATE_KEY_ERROR.getCode().equals(dto.getHead().getResponseCode())) {
			throw new ManagerException("改订单已被冲正/调账...");
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
